package com.example.school.controller.admin;

import com.example.school.dto.*;
import com.example.school.service.AdminService;
import com.example.school.utils.common.enums.Code;
import com.example.school.utils.common.result.Result;
import com.example.school.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/admin_login")
    public Result adminLogin(@RequestBody AdminLoginDto dto){
        return adminService.adminLogin(dto);
    }

    @PostMapping("/save_admin")
    public Result saveAdmin(@RequestBody SaveAdminDto dto){
        return adminService.saveAdmin(dto);
    }

    @GetMapping("/remove_admin")
    public Result removeAdmin(@RequestParam("id") Long id){
        Long check = BaseContext.getCurrentId();
        if (check==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        boolean checkRemove = adminService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/update_get_admin")
    public Result updateGetAdmin(@RequestParam("id") Long id){
        Long adminId = BaseContext.getCurrentId();
        if (adminId==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(adminService.getById(id));
    }

    @PostMapping("/update_admin")
    public Result updateAdmin(@RequestBody AdminUpdateDto dto){
        return adminService.updateAdmin(dto);
    }

    @PostMapping("/get_admin_list")
    public Result getAdminList(@RequestBody AdminGetListDto dto){
        return adminService.getAdminList(dto);
    }

    @GetMapping("admin_login_out")
    public Result adminLoginOut(){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        BaseContext.removeCurrentId();
        return Result.okResult(Code.SUCCESS);
    }

}
